package mobilesmil.view;

import mobilesmil.activity.composer.ComposerActivity;
import mobilesmil.activity.composer.RegionPositioningActivity;
import mobilesmil.data.SmilImageResource;
import mobilesmil.data.SmilRegion;
import mobilesmil.data.SmilResource;
import mobilesmil.data.SmilTextResource;
import mobilesmil.userinterface.MoveableResourceRect;
import mobilesmil.utils.ColorUtils;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

/**
 * {@link View} used by the {@link RegionPositioningActivity} to display a canvas and a {@link MoveableResourceRect}
 * which gives the user the ability to modify the position and dimensions of the {@link SmilRegion} of the {@link SmilResource}
 * they are currently editing.
 * @author Ryan Ware
 *
 */
public class RegionPositioningView extends View {

	private SmilResource mEditingResource = ComposerActivity.getEditingResource();
	
	private MoveableResourceRect mRegionRect; // a border around image/text resources or a gray box to represent video resources
	private Paint mRegionRectPaint; // for the mRegionRect
	private Paint mRegionResizerPaint; // for the small resizing rectangle of mRegionRect
	private SmilResource mPreviewOfResource; // if resource is image or text, displays it on positioning screen
	private RegionPositioningActivity mContext;
	
	private final int CANVAS_BACKGROUND_COLOR = ComposerActivity.getEditingDataSet().getBackgroundColor().getColor();
	
	private boolean mDraggingResource = false;
	private boolean mResizingResource = false;
	private boolean mDrawRegionRect = false;
	private int mActionDownX = 0;
	private int mActionDownY = 0;
	
	public RegionPositioningView(Context context) {
		super(context);
		this.mContext = ((RegionPositioningActivity)context);
		setFocusable(true);
		
		mRegionResizerPaint = new Paint();
		mRegionResizerPaint.setColor(ColorUtils.getComplimentColor(CANVAS_BACKGROUND_COLOR));
		
		mRegionRectPaint = new Paint();
		mRegionRectPaint.setColor(ColorUtils.getComplimentColor(CANVAS_BACKGROUND_COLOR));
		
		if(!mEditingResource.getType().equals(SmilResource.TYPE_VIDEO))
		{
			mRegionRectPaint.setStyle(Style.STROKE);
			mRegionRectPaint.setStrokeWidth(1);
			
			if(mEditingResource.getType().equals(SmilResource.TYPE_IMAGE)) { // an image resource
				mPreviewOfResource = new SmilImageResource(mEditingResource.getSource(), 
						new SmilRegion(mEditingResource.getSmilRegion()), mEditingResource.getBeginTime(), mEditingResource.getEndTime());
			}
			else { // a text resource
				mPreviewOfResource = new SmilTextResource(mEditingResource.getSource(), 
						new SmilRegion(mEditingResource.getSmilRegion()), mEditingResource.getBeginTime(), mEditingResource.getEndTime());
			}
		}
		else { // video resource
			mDrawRegionRect = true;
			mRegionRectPaint.setStyle(Style.FILL);
		}

		int left = mEditingResource.getSmilRegion().getRect().left;
		int top = mEditingResource.getSmilRegion().getRect().top;
		int width = mEditingResource.getSmilRegion().getRect().width();
		int height = mEditingResource.getSmilRegion().getRect().height();

		mRegionRect = new MoveableResourceRect(context, left, top, width, height);
	}
	
    @Override
	public void onWindowFocusChanged(boolean hasWindowFocus) {
		super.onWindowFocusChanged(hasWindowFocus);
		if(hasWindowFocus) {
			invalidate(); // redraw canvas after region properties dialog is closed
		}
	}

	@Override
    protected void onDraw(Canvas canvas) {
    	canvas.drawColor(CANVAS_BACKGROUND_COLOR);
    	
    	if(mContext.isRegionUpdatedByDialog()) // must update the MoveableResourceRect on the screen
    	{
	    	int newRegionHeight = mContext.getRegionHeight();
	    	int newRegionWidth = mContext.getRegionWidth();
	    	int newRegionTop = mContext.getRegionTop();
	    	int newRegionLeft = mContext.getRegionLeft();
	    	mRegionRect.updateCoordinatesWithinCanvasBoundaries(newRegionLeft, newRegionTop);
	    	mRegionRect.updateDimensionsWithinCanvasBoundaries(newRegionWidth, newRegionHeight);
	    	mContext.setRegionUpdatedByDialog(false);
    	}
    	
    	if(mPreviewOfResource != null) // draw preview of resource if image or text
    	{
    		mPreviewOfResource.getSmilRegion().setRect(mRegionRect.getRect());
    		if(mEditingResource.getType().equals(SmilResource.TYPE_TEXT)) {
    			((SmilTextResource)mPreviewOfResource).updateText();
    		}
    		mPreviewOfResource.play(canvas);
    	}
    	
    	if(mDrawRegionRect)
    		canvas.drawRect(mRegionRect.getRect(), mRegionRectPaint);
    	canvas.drawRect(mRegionRect.getResizeRect(), mRegionResizerPaint);
    }
    
    public boolean onTouchEvent(MotionEvent event) {
        int height = mRegionRect.getRect().height();
    	int width = mRegionRect.getRect().width();
    	int left = mRegionRect.getRect().left;
    	int top = mRegionRect.getRect().top;

        switch (event.getAction()) 
        {
	        case MotionEvent.ACTION_DOWN: // touch down so check if the finger is inside the Region
	        	mActionDownX = (int)event.getX();
	        	mActionDownY = (int)event.getY();
	        	
	        	if(mRegionRect.getResizeRect().contains(mActionDownX, mActionDownY)) 
	        	{ // user touched the resizer rectangle - wants to resize it
	    			mResizingResource = true;
	    			mDraggingResource = false;
	    		}
	    		else if(mRegionRect.getRect().contains(mActionDownX, mActionDownY)) 
	    		{ // user touched the region - wants to drag it
	    			mRegionResizerPaint.setAlpha(0); // hide resizer while dragging
	    			mDraggingResource = true;
	    			mResizingResource = false;
	    		}
	        	
	        	if(!mEditingResource.getType().equals(SmilResource.TYPE_VIDEO)) {
	        		mDrawRegionRect = true; // want to draw a preview of non-video resources
	        	}
	    		
	            break;
	
	        case MotionEvent.ACTION_MOVE: // user is dragging finger
	            int draggedToX = (int)event.getX();
	            int draggedToY = (int)event.getY();
	        	int dragOffsetX = mActionDownX - draggedToX;
	            int dragOffsetY = mActionDownY - draggedToY;
	            
	        	if (mDraggingResource) { // dragging the region
	            	mRegionRect.updateCoordinatesWithinCanvasBoundaries(left - dragOffsetX, top - dragOffsetY);
	            }
	            else if(mResizingResource){ // resizing the region
	            	mRegionRect.updateDimensionsWithinCanvasBoundaries(draggedToX - left, draggedToY - top);
	            }
	        	mActionDownX = draggedToX;
	        	mActionDownY = draggedToY;
	            break;
	
	        case MotionEvent.ACTION_UP: // user removed finger
	        	if(!mEditingResource.getType().equals(SmilResource.TYPE_VIDEO)) {
	        		mDrawRegionRect = false;
	        	}
	        	// notify user of new coordinates or dimensions
	        	if(mDraggingResource)
	        		Toast.makeText(mContext, "Coordinates: (" + left + "," + top + ")", Toast.LENGTH_SHORT).show();
	        	else if(mResizingResource)
	        		Toast.makeText(mContext, "Dimensions: " + width + " W x " + height + " H", Toast.LENGTH_SHORT).show();
	        	
	        	mDraggingResource = false;
	        	mResizingResource = false;
	        	mRegionResizerPaint.setAlpha(255); // display resizer rectangle
	            break;
        }
        
        // redraw the canvas
        invalidate();
        return true;
    }

	public MoveableResourceRect getRegionRect() {
		return mRegionRect;
	}

	public SmilResource getMoveablePreviewOfResource() {
		return mPreviewOfResource;
	}

	public void setMoveablePreviewOfResource(SmilResource moveablePreviewOfResource) {
		this.mPreviewOfResource = moveablePreviewOfResource;
	}
    
}
